package edu.univercidade.tcc.server.validator;

import static java.lang.annotation.ElementType.TYPE;
import static java.lang.annotation.RetentionPolicy.RUNTIME;

import java.lang.annotation.Documented;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;

import javax.validation.Constraint;
import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import javax.validation.Payload;

import edu.univercidade.tcc.server.model.Issue;

@Target({ TYPE })
@Retention(RUNTIME)
@Constraint(validatedBy = ProjectAllowsChosenIssueTracker.Validator.class)
@Documented
public @interface ProjectAllowsChosenIssueTracker {
	String message() default "O projeto escolhido deve permitir o tipo de tarefa.";

	Class<?>[] groups() default {};

	Class<? extends Payload>[] payload() default {};

	class Validator implements
			ConstraintValidator<ProjectAllowsChosenIssueTracker, Object> {
		@Override
		public void initialize(ProjectAllowsChosenIssueTracker arg0) {

		}
		
		@Override
		public boolean isValid(Object target, ConstraintValidatorContext context) {
			Issue issue = (Issue) target;
			return issue.getProject().getTrackers().contains(issue.getTracker());
		}
	}
}